package com.lijie.ljblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijie.ljblog.common.BlogConstants;
import com.lijie.ljblog.mapper.BlogMapper;
import com.lijie.ljblog.model.Blog;
import com.lijie.ljblog.service.BlogService;
import com.lijie.ljblog.util.QueryWrapperUtil;
import com.lijie.ljblog.util.RedisUtils;
import com.lijie.ljblog.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijie
 * @since 2021-08-28
 */
@Service
@Slf4j
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {
    @Autowired
    BlogMapper blogMapper;
    @Autowired
    QueryWrapperUtil queryWrapperUtil;
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    ExecutorService executorService;


    @Override
    /** 拿文章数据，不包括公告
     * @Title: selBlogPage
     * @Description: ()
     * @Author:lijie
     * @since 2021/11/3 10:37
     * @Version:1.1.0
     */
    public PageInfo<Blog> selBlogPage(Integer page, Blog blog) {
        QueryWrapper<Blog> queryWrapper = queryWrapperUtil.getBlogQueryWrapper(blog);
        PageHelper.startPage(page, 10);
        queryWrapper.ne("blogClassify", 4);
        List<Blog> blogs = blogMapper.selectList(queryWrapper);
        return new PageInfo<>(blogs);
    }

    /**
     * 拿公告数据
     *
     * @Title: selNotice
     * @Description: ()
     * @Author:lijie
     * @Version:1.1.0
     * @since 2021/11/3 11:03
     */
    @Override
    public PageInfo<Blog> selNotice(Integer page, Blog blog) {
        QueryWrapper<Blog> queryWrapper = queryWrapperUtil.getBlogQueryWrapper(blog);
        PageHelper.startPage(page, 3);
        queryWrapper.eq("blogClassify", 4);
        List<Blog> blogs = blogMapper.selectList(queryWrapper);
        return new PageInfo<>(blogs);
    }

    /**
     * @Title: addReadNums
     * @Description: (通过博客Id ， 增加该博客的阅读量 ; 重构将文章的阅读量更新存储在redis中)
     * @Author:lijie
     * @Version:1.1.0
     * @since 2021/11/22 16:43
     */
    @Override
    public Result<Blog> getByIdAndAddReadNums(Integer blogId) {
        Result<Blog> result = new Result<>();
        if (blogId != null) {
            synchronized (blogId.toString().intern()) {
                try {
                    //以blogId+'readNums'作为key;
                    String key = BlogConstants.READ_NUMS + blogId;
                    Long aLong = redisUtils.incrBy(key, 1);
                    Integer readNums = Integer.parseInt(String.valueOf(aLong));
                    Blog blog = blogMapper.selectById(blogId);
                    blog.setReadNums(readNums);
                    executorService.submit(()-> {
                        log.info("——进线程池——");
                        blogMapper.updateById(blog);
                        log.info("——线程池任务结束——");
                    });
                    result.setData(blog);
                } catch (Exception e) {
                    result.setErrored("出错了" + e);
                    return result;
                }
            }
        } else {
            result.setErrored();
        }
        return result;
    }

    @Override
    public Result<Blog> getById(Integer id) {
        Result<Blog> blogResult = new Result<>();
        Blog blog = blogMapper.selectById(id);
        if (blog != null && blog.getId() != null) {
            blogResult.setStatus(Result.STATUS_CODE_SUCCESS);
            blogResult.setData(blog);
        } else {
            blogResult.setStatus(Result.STATUS_CODE_500);
        }
        return blogResult;
    }

    /**
     * @Title: getBlogVisibleNums
     * @Description: (获得首页课件博客的总数)
     * @Author:lijie
     * @Version:1.1.0
     * @since 2021/11/25 17:08
     */
    @Override
    public Integer getBlogVisibleNums() {
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("blogStatus", 1);
        return blogMapper.selectCount(queryWrapper);
    }

    /**
     * 查询推荐博客
     *
     * @return
     */
    @Override
    public Result<List<Blog>> hotBlog() {
        List<Blog> blogs = blogMapper.hotBlogList();
        return Result.ok(blogs);
    }


}
